import { Model } from '@/utils/dva-helper'
import { getCookie, setCookie } from '@/utils/cookie'

// 初始化Model层
export class UserState {
	menuCurrent: any

	permission: any = getCookie('permission')

	headerMenuKey: any

	headerMenu: any

	menu: any = []

	userName: any = getCookie('userName')

	userId: any = getCookie('userId')

	orgId: any = getCookie('orgId')

	// currentMerchantId: any = getCookie('orgId')
	currentMerchantId: any = getCookie('currentMerchantId')

	menuTree: any = []

	permissionID: any = getCookie('permissionID')
}

const model: Model<UserState> = {
	namespace: 'user',
	state: new UserState(),
	// 订阅
	subscriptions: {},
	effects: {},
	reducers: {
		updateUserInfo(state, { payload }) {
			setCookie('userId', payload.userId, 7)
			setCookie('userName', payload.userName, 7)
			return { ...state, ...payload }
		},
		updateOrgId(state, { payload }) {
			setCookie('orgId', payload.orgId, 7)
			return { ...state, ...payload }
		},
		updateCurrentMerchantId(state, { payload }) {
			setCookie('currentMerchantId', payload.currentMerchantId, 7)
			return { ...state, ...payload }
		},
		updateMenu(state, { payload }) {
			return { ...state, ...payload }
		},
		updateHeaderMenu(state, { payload }) {
			return { ...state, ...payload }
		},
		updateSelectHeaderMenu(state, { payload }) {
			setCookie('headerMenuKey', payload.headerMenuKey, 7)
			return { ...state, ...payload }
		},
		updatePermission(state, { payload }) {
			setCookie('permission', payload.permission, 7)
			return { ...state, ...payload }
		},

		updateMenuCurrent(state, { payload }) {
			return { ...state, ...payload }
		},

		updateMenuTree(state, { payload }) {
			return { ...state, ...payload }
		},
	},
}
export default model
